package com.cn.web.controller.fy;

import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.lbj.ParameterClassificationTable;
import com.cn.bean.pojo.yw.BusinessMatterClassificationTable;
import com.cn.bean.pojo.yw.ExpenseItemInfoTable;
import com.cn.config.config.Result;
import com.cn.service.server.PartsManagement.BusinessMatterClassificationService;
import com.cn.service.server.PartsManagement.ExpenseItemInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Slf4j
@Transactional
public class BusinessMatterClassificationController {
    @Autowired
    private BusinessMatterClassificationService businessMatterClassificationService;

    @RequestMapping("/select/BusinessMatterClassification/Aside")
    @ResponseBody
    public List<BusinessMatterClassificationTable> selectBusinessMatterClassificationAside() {
        List<BusinessMatterClassificationTable> originalData = businessMatterClassificationService.selectBusinessMatterAside();
        Map<Integer, BusinessMatterClassificationTable> nodeMap = new HashMap<>();

        // Step 1: 构建映射
        for (BusinessMatterClassificationTable nodeData : originalData) {
            BusinessMatterClassificationTable node = new BusinessMatterClassificationTable();
            node.setAutoid(nodeData.getAutoid());
            node.setCode(nodeData.getCode());
            node.setDescription(nodeData.getDescription());
            node.setId(nodeData.getId());
            node.setIsLeaf(nodeData.getIsLeaf());
            node.setLevel(nodeData.getLevel());
            node.setName(nodeData.getName());
            node.setLabel(nodeData.getName());
            node.setParentId(nodeData.getParentId());
            node.setStatus(nodeData.getStatus());
            nodeMap.put(node.getId(), node);
        }

        // Step 2: 构建树状结构
        for (BusinessMatterClassificationTable nodeData : originalData) {
            BusinessMatterClassificationTable node = nodeMap.get(nodeData.getId());
            if (node.getParentId() != 0) {
                BusinessMatterClassificationTable parent = nodeMap.get(node.getParentId());
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }

        // Step 3: 找到根节点并返回
        List<BusinessMatterClassificationTable> roots = new ArrayList<>();
        for (BusinessMatterClassificationTable node : nodeMap.values()) {
            if (node.getParentId() == 0) {
                roots.add(node);
            }
        }

        return roots;

    }
    @RequestMapping("/select/Aside/All")
    @ResponseBody
    public List<BusinessMatterClassificationTable> selectAsideAll(@RequestBody JSONObject jsonObject) {

        return businessMatterClassificationService.selectBusinessMatterAside();
    }

    @RequestMapping("/select/BusinessMatterClassification/Aside/All")
    @ResponseBody
    public List<BusinessMatterClassificationTable> selectBusinessMatterClassificationAsideAll(@RequestBody JSONObject jsonObject) {
        Integer parentID = jsonObject.getInteger("parentID");
        List<BusinessMatterClassificationTable> businessTypeTables = businessMatterClassificationService.selectBusinessMatterAside();

        List<BusinessMatterClassificationTable> result = new ArrayList<>();
        for (BusinessMatterClassificationTable type : businessTypeTables) {
            if (type.getId() == parentID) {
                // 找到与指定parentID匹配的根节点，调用递归函数获取其所有子节点
                result.add(type);
                addChildNodes(type, businessTypeTables, result);
            }
        }

        return result;
    }

    // 递归函数用于获取指定parentID下的所有子节点
    private void addChildNodes(BusinessMatterClassificationTable parent, List<BusinessMatterClassificationTable> allNodes, List<BusinessMatterClassificationTable> result) {
        for (BusinessMatterClassificationTable node : allNodes) {
            if (node.getParentId() == parent.getId()) {
                // 如果当前节点是指定parentID的子节点，将其添加到结果列表中
                result.add(node);
                // 继续递归查找当前节点的子节点
                addChildNodes(node, allNodes, result);
            }
        }
    }
    //
    @RequestMapping("/select/BusinessMatterClassification")
    @ResponseBody
    public List<BusinessMatterClassificationTable> selectBusinessMatterClassification(@RequestBody BusinessMatterClassificationTable businessMatterClassificationTable) {

        List<BusinessMatterClassificationTable> businessTypeTables = businessMatterClassificationService.selectBusinessMatter(businessMatterClassificationTable);
        return businessTypeTables;
    }

    @RequestMapping("/add/BusinessMatterClassification")
    @ResponseBody
    public Result addBusinessMatterClassification(@RequestBody JSONObject jsonObject) {
        BusinessMatterClassificationTable formAsid = jsonObject.getObject("formAsid", BusinessMatterClassificationTable.class);
        if (formAsid.getParentId() == 0) {
            formAsid.setLevel(1);
            formAsid.setParentId(1);
        }

        Result result = businessMatterClassificationService.addBusinessMatter(formAsid.getName(), formAsid.getDescription(), formAsid.getStatus(), formAsid.getLevel(), formAsid.getParentId(), 0);
//        return businessTypeService.addBusinessType();
        return result;
    }
    //
    @RequestMapping("/delete/BusinessMatterClassification")
    @ResponseBody
    public Result deleteBusinessMatterClassification(@RequestBody JSONObject jsonObject) {
        BusinessMatterClassificationTable asid=new BusinessMatterClassificationTable();
        asid = jsonObject.getObject("formAsid", BusinessMatterClassificationTable.class);
        if (asid.getId()==0||asid.getId()==1){
            return Result.failure("业务类型字段不可修改");
        }
        int id = asid.getId();
        return businessMatterClassificationService.delBusinessMatter(id);
    }

    @RequestMapping("/update/BusinessMatterClassification")
    @ResponseBody
    public Result updateBusinessMatterClassification(@RequestBody JSONObject jsonObject) {
        BusinessMatterClassificationTable formAsid = jsonObject.getObject("formAsid", BusinessMatterClassificationTable.class);
        if (formAsid.getId()==0||formAsid.getId()==1){
            return Result.failure("业务类型字段不可删除");
        }
        Result result = businessMatterClassificationService.updateBusinessMatter(formAsid.getId(), formAsid.getCode(), formAsid.getName(), formAsid.getDescription(), formAsid.getStatus(), formAsid.getLevel(), formAsid.getParentId(), 0);
        return result;
    }
}
